<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: posts.php Sat Jul 18 13:12:25 CST 2009
 */

if ( !defined("XOOPS_ROOT_PATH") ) exit;
if ( !class_exists("xoGroups") ) {
	include dirname(dirname(__FILE__))."/include/groups.php";
}
class groupsPosts extends XoopsObject {
	public function __construct() {
		$this->initVar("post_id", XOBJ_DTYPE_INT, null, false); // 主题ID
		$this->initVar("group_id", XOBJ_DTYPE_INT,0,true); // 所属群组ID
		$this->initVar("column_id", XOBJ_DTYPE_INT,0,true); // 分栏ID
		$this->initVar("tid", XOBJ_DTYPE_INT,0,true); // 是主题还是回复 0 表主题 >0 表示对应主题的回复 
		$this->initVar("uid", XOBJ_DTYPE_INT,0,true); // 主题作者
		$this->initVar("post_first", XOBJ_DTYPE_INT,0,true); // 是否是主题 1 是， 0 不是
		$this->initVar("post_author", XOBJ_DTYPE_TXTBOX,null,false); // 作者
		$this->initVar("post_create_time", XOBJ_DTYPE_INT,0,false); // 发表时间
		$this->initVar("post_subject", XOBJ_DTYPE_TXTBOX,null,true); // 主题标题
		$this->initVar("post_content", XOBJ_DTYPE_TXTAREA,null,true); // 主题内容
		$this->initVar("post_user_sign", XOBJ_DTYPE_TXTBOX,null,false); // 用户签名
		$this->initVar("post_user_ip", XOBJ_DTYPE_TXTBOX,null,false); // 发表者IP
		$this->initVar("post_attachments", XOBJ_DTYPE_INT,null,false); // 主题的附件 0 表示没有
		$this->initVar("post_status", XOBJ_DTYPE_INT,null,false); // 主题的状态 0 正常 1被锁 2关闭
	}	
	
	public function createTime() {
	    return formatTimestamp($this->getVar("post_create_time"));
	}
}

class groupsPostsHandler extends XoopsPersistableObjectHandler
{
	public function __construct($db) {
        parent::__construct($db,"groups_posts","groupsPosts","post_id");
    }
    
    public function setPost( array $params ) {
        $myts = MyTextSanitizer::getInstance();
        if ( empty($params) || !is_array($params) ) return false;
    	$post_id = isset($params["post_id"]) ? intval($params["post_id"]) : 0 ;
        if ( !empty($post_id) ) {
            $obj = $this->get($post_id);
        } else {
            $obj = $this->get();
            $obj->setVar("post_create_time",time());
        }
    	foreach ( $params as $key => $val ) {
			if ( !isset($obj->vars[$key]) || $myts->stripSlashesGPC($val) == $obj->getVar($key) ) continue;
			$obj->setVar($key, $val);
		}
    	if ( $post_id = $this->insert($obj) ) {
    	    return $post_id;
    	}
    	return false;
    }
    
    public function getPosts( array $params, $asObj=true ) {
        $criteria = xoGroups::getCriteria($params);
        $fields = ( isset($params["fields"]) && is_array($params["fields"]) ) ? $params["fields"] : null ;
        $ret = $this->getAll($criteria, $fields, $asObj);
        return $ret;
    }
    
	public function getPostCount( array $params ) {
		$criteria = xoGroups::getCriteria($params);
		return $this->getCount($criteria);
	}
	
    /**
     * for page nav
     *
     * @param array $params
     * @return string $pagenav
     */
    public function getPageNav( array $params ) {
        $pagenav = "";
        $criteria = xoGroups::getCriteria($params);
        $counts = $this->getCount($criteria);
        $extra = isset($params["extra"]) ? trim($params["extra"]) : "";
        if( $counts > $params["limit"]){	
        	$nav = new XoopsPageNav($counts, $params["limit"], $params["start"], "start",$extra);
        	$pagenav = $nav->renderNav();
        }
        return $pagenav;
    }
        
    public function getPostInStartPage($tid, $post_id, $limit=10) {
        $criteria = new CriteriaCompo(new Criteria("tid",$tid));
        $counts = $this->getIds($criteria);
        if ( $counts && in_array($post_id,$counts)) {
            foreach ( $counts as $k=>$v) {
                if ( $v == $post_id ) {
                    $val = $k ;
                    break;
                }
            }
            $startval = ceil( ($k+1) / $limit);
            return $startval < 1 ? 0 : $startval;
        }
        return 0;
    }
    
    public function getnoHtml($str) {
    	return preg_replace( "@<(.*?)>@is", "", $str );
    }
    
 }
    